﻿using StableDiffusionTools.Interop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StableDiffusionTools.Controls
{
    public class RichTextBoxEx : RichTextBox
    {
        public bool Focusable { get; set; } = true;

        public RichTextBoxEx() : base()
        {
            SetStyle(
                    ControlStyles.AllPaintingInWmPaint |
                    ControlStyles.OptimizedDoubleBuffer |
                    ControlStyles.ResizeRedraw, true);
        }

        protected override void WndProc(ref Message m)
        {
            //Fix mushy scrolling
            if (m.Msg == (int)WindowMessage.MouseWheel)
            {
                int scrollLines = SystemInformation.MouseWheelScrollLines;
                for (int i = 0; i < scrollLines; i++)
                {
                    if ((int)m.WParam > 0)
                        User32.SendMessage(Handle, WindowMessage.VerticalScroll, (IntPtr)0, IntPtr.Zero);
                    else
                        User32.SendMessage(Handle, WindowMessage.VerticalScroll, (IntPtr)1, IntPtr.Zero);
                }
                return;
            }
            else if(!Focusable && m.Msg == (int)WindowMessage.SetFocus)
            {
                return;
            }

            base.WndProc(ref m);
        }
    }
}
